import java.util.ArrayDeque;
import java.util.Deque;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 17655
 * Date: 2024-12-19
 * Time: 10:56
 */
public class test1 {
    /*
    leetCode 227  基本计算器II
     */
    // 让所有在栈里面的元素都执行加法运算
    // 遇到乘除就把栈顶元素提出，进行运算再放入栈中
    // 1.遇到操作符：更新操作符
    // 2.遇到数字，根据数字进行分情况讨论
    public int calculate(String ss) {
        Deque<Integer> s1 = new ArrayDeque<>();
        char op = '+';
        int i=0;
        int n=ss.length();
        char[] s =ss.toCharArray();
        while(i<n){
            if(s[i]==' '){
                i++;
            }else if(s[i]>='0' && s[i]<='9'){
                int tmp = 0;
                while(i<n && s[i]>='0' && s[i]<='9'){
                    tmp = tmp*10+(s[i]-'0');
                    i++;
                }
                if(op =='+'){
                    s1.push(tmp);
                }else if(op=='-'){
                    s1.push(-tmp);
                }else if(op=='*'){
                    s1.push(s1.pop() * tmp);
                }else{
                    s1.push(s1.pop() / tmp);
                }
            }else{
                op = s[i];
                i++;
            }
        }
        int ret = 0;
        while(!s1.isEmpty()){
            ret += s1.pop();
        }
        return ret;
    }
}
